define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/dialog",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "Date", "DateCN", "css!DateCss",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, Dialog, SelectOrg, SearchBlock) {

    var sysPath = getServer() + "/static/app/fxpg/fxDaynight";

    var fxDayNightPercentFormHtml = sysPath + "/views/fxDayNightPercentForm.html";
    var getFxDayNightPercentController = function () {
        return getServer() + "/fxDayNightPercents";
    };

    /**
     * 页面初始化
     */
    var init = function () {
        initSearchBlock();
        queryBtnBind();
        initHouseTypeSelect();
        createFxDayNightPercentGrid();
    };
    var initHouseTypeSelect = function () {
        $.ajax({
            url: getFxDayNightPercentController() + "/getValidDictItemsByDictCode/" + "houseType",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("houseTypeSelect");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };

    var initHouseTypeSelectFrom = function () {
        $.ajax({
            url: getFxDayNightPercentController() + "/getValidDictItemsByDictCode/" + "houseType",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("houseType");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };

    var initSearchBlock = function () {
        SearchBlock.init("searchBlock");
    };
    var queryBtnBind = function () {
        $("#btnSearch").click(function () {
            createFxDayNightPercentGrid();
        });
        $("#btnReset").click(function () {
            $("#houseTypeSelect").val("");
        });
    };
    var createFxDayNightPercentGrid = function () {
        $("#fxDayNightPercentGrid").datagrid({
            url: getFxDayNightPercentController() + "/queryFxDayNightPercents",
            method: "GET",
            fitColumns: true,
            autoRowHeight: false,
            rownumbers: false,
            columns: [[
                {field: "ck", checkbox: true},
                {
                    field: 'houseType',
                    title: '房屋类别',
                    width: '18%',
                    align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxDayNightPercent(\'' + rowData.id + '\');"> ' + rowData.houseType + ' </a> '
                    }
                },
                {
                    field: 'nightMin',
                    title: '夜间最小值',
                    width: '20%',
                    align: 'center',
                },
                {
                    field: 'nightMax',
                    title: '夜间最大值',
                    width: '20%',
                    align: 'center',
                },
                {
                    field: 'dayMin',
                    title: '白天最小值',
                    width: '20%',
                    align: 'center',
                },
                {
                    field: 'dayMax',
                    title: '白天最大值',
                    width: '20%',
                    align: 'center',
                }
            ]],
            toolbar: [{
                iconCls: 'fa fa-plus-circle',
                text: "添加",
                handler: function () {
                    addFxDayNightPercent();
                }
            }, {
                iconCls: 'fa fa-download',
                text: "导出",
                handler: function () {
                    exportForm();
                }
            }, {
                iconCls: 'fa fa-trash-o',
                text: "删除",
                handler: function () {
                    deleteFxDayNightPercent();
                }
            }],
            queryParams: {
                houseType: $("#houseTypeSelect").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };
    var formValidator = function () {
        $("#fxDayNightPercentForm").validate({
            rules: {
                houseType: {
                    required: true,
                },
                dayMin: {
                    required: true,
                },
                nightMin: {
                    required: true,
                },
                dayMax: {
                    required: true,
                },
                nightMax: {
                    required: true,
                },
            },
            messages: {
                houseType: {
                    required: "房屋类别不允许为空!",
                },
                dayMin: {
                    required: "白天最小值不允许为空!",
                },
                nightMin: {
                    required: "夜间最小值不允许为空!",
                },
                dayMax: {
                    required: "白天最大值不允许为空!",
                },
                nightMax: {
                    required: "夜间最大值不允许为空!",
                },
            }
        });
    };
    var getFxDayNightPercent = function (id) {
        $.ajax({
            url: getFxDayNightPercentController() + "/" + id,
            type: "get",
            success: function (data) {
                Tool.deserialize("fxDayNightPercentForm", data);
            }
        });
    };

    var addFxDayNightPercent = function () {
        var slidebar = Util.slidebar({
            url: fxDayNightPercentFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                initHouseTypeSelectFrom();
                formValidator();
                $("#saveBtn").on("click", function () {
                    if ($("#fxDayNightPercentForm").valid()) {
                        var form = Tool.serialize("fxDayNightPercentForm");
                        $.ajax({
                            url: getFxDayNightPercentController(),
                            contentType: "application/json",
                            data: JSON.stringify(form),
                            type: "post",
                            success: function (data) {
                                if (data.message == "房屋类别重复") {
                                    repeat(form, slidebar);
                                } else {
                                    Util.alert(data.message);
                                    slidebar.close();
                                    createFxDayNightPercentGrid();
                                }
                            }
                        });
                    }
                });
            }
        });
    };

    /**
     * 根据房屋类别判断存在后，覆盖原数据
     */
    var repeat = function (form, slidebar) {
        debugger;
        var buttons = [];
        buttons.push({
            name: '确定',
            callback: function () {
                //点击确定
                debugger;
                $.ajax({
                    url: getFxDayNightPercentController() + "/cover",
                    contentType: "application/json",
                    data: JSON.stringify(form),
                    type: "put",
                    success: function (data) {
                        Util.alert(data.message);
                        slidebar.close();
                        createFxDayNightPercentGrid();
                    }
                });
                dialog.hide();
            }
        });
        var dialog = Dialog({
            id: "repeatDialog",
            cache: false,
            title: '重复覆盖', //dialog标题
            width: "200px",
            //height: "50px",
            body: '<div id="TeamGrid">' +
                '<span>房屋类别：' + form.houseType + '重复，是否覆盖？<span/>' +
                '</div>',
            buttons: buttons,
        });
        dialog.show();
    };

    /**
     * 导出
     */
    var exportForm = function () {
        let houseType = $("#houseTypeSelect").val();
        window.location.href = getFxDayNightPercentController() + "/export?houseType=" + houseType;
    };

    window._editFxDayNightPercent = function (fxDayNightPercentId) {
        var slidebar = Util.slidebar({
            url: fxDayNightPercentFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                initHouseTypeSelectFrom();
                formValidator();
                getFxDayNightPercent(fxDayNightPercentId);
                $("#saveBtn").on("click", function () {
                    if ($("#fxDayNightPercentForm").valid()) {
                        var data = Tool.serialize("fxDayNightPercentForm");
                        $.ajax({
                            url: getFxDayNightPercentController(),
                            contentType: "application/json",
                            data: JSON.stringify(data),
                            type: "put",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                createFxDayNightPercentGrid();
                            }
                        });
                    }
                });
            }
        });
    };
    var deleteFxDayNightPercent = function () {
        var rows = $("#fxDayNightPercentGrid").datagrid("getSelections");
        if (rows == null || rows.length == 0) {
            Util.alert("请选择一行数据!");
            return;
        }
        Util.confirm("是否要删除选中的数据?", function () {
            var ids = "";
            $.each(rows, function (i, row) {
                ids += row.id + ",";

            });
            ids = ids.substr(0, ids.length - 1);
            $.ajax({
                url: getFxDayNightPercentController(),
                data: {
                    ids: ids
                },
                type: "delete",
                success: function (data) {
                    createFxDayNightPercentGrid();
                }
            });
        }, function () {
            return;
        });

    };

    return {
        init: init
    };
});
